<?php
defined('IN_ZEEGING') or die('Access Denied');
class Base{
	static public function  is_cli(){
		return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
	}
	static public function getUserIP()
	{
		//Default value in case running on CLI
		$userip = '127.0.0.1';

		//If running in web context
		if(array_key_exists('REMOTE_ADDR', $_SERVER))
		{
			//Found a web user's ip
			$userip = $_SERVER['REMOTE_ADDR'];

	        if(array_key_exists("HTTP_CF_CONNECTING_IP", $_SERVER))
	        {
	            //If cloudflare
	            $userip = $_SERVER['HTTP_CF_CONNECTING_IP'];
	        }
			else if(array_key_exists("HTTP_X_FORWARDED_FOR", $_SERVER))
			{
				//If running behind proxy
				$userip = $_SERVER['HTTP_X_FORWARDED_FOR'];
				if(strstr($userip, ",") !== false)
				{
				    //If multiple x-forwarded-for headers
					$parts = explode(",", $userip);
					$userip = $parts[0];

				}
	            if(strstr($userip, ":") !== false)
	            {
	                //If multiple x-forwarded-for headers
	                $parts = explode(":", $userip);
	                $userip = $parts[0];

				}
			}
		}

		return $userip;
	}
	static public function log($content,$logName='LOG'){
		$filename=ZEEGING_PATH."log".DIRECTORY_SEPARATOR.date("Ymd")."_".$logName.".log";
		file_put_contents($filename, date('Y-m-d H:i:s')."\t".$content."\r\n",FILE_APPEND);
	}

	protected function loadConfigs($file){
		$path=ZEEGING_PATH."Config".DIRECTORY_SEPARATOR.$file.".cfg.php";
		if (file_exists($path)){
			return include $path;
		}
		else {
			return NULL;
		}
	}
	/*
	//旧的加载类方法，缺点是不能传多个参数到构造函数
	protected function loadMyClass($file,$arg=NULL){
		$path=ZEEGING_PATH."Librarys".DIRECTORY_SEPARATOR.$file.".class.php";
		if(file_exists($path)){
			$className=$file;
			if(!class_exists($className)){
				include $path;
			}//Class has been existing.Can not declare it.
			if(class_exists($className)){
				return new $className($arg);
			}
			else{
				exit('Class does not exist.');
			}
		}
		else{
			exit('Model does not exist.');
		}
	}
	*/
	//新的方法可以传任意参数到构造函数，其中第一个参数是类名，第二个参数开始将全部传递给构造函数
	protected function loadMyClass(){
		$args=func_get_args();
		$file=array_shift($args);//弹出第一个参数，作为类名
		$path=ZEEGING_PATH."Librarys".DIRECTORY_SEPARATOR.$file.".class.php";
		if(file_exists($path)){
			$className=$file;
			if(!class_exists($className)){
				include $path;
			}//Class has been existing.Can not declare it.
			if(class_exists($className)){
				//以下三行目的是构造参数列表
				//本方法参考文章：http://taoyh163.blog.163.com/blog/static/1958035620141371710957
				$keys=array_keys($args);
				array_walk($keys,create_function('&$val,$key,$prefix','$val=$prefix.$val;'),'$arg_');//为key加上$前缀，否则会报错
				$paramsStr=implode(',',$keys);
				
				$newFunc=create_function($paramsStr,"return new $className($paramsStr);");
				return call_user_func_array($newFunc,$args);
			}
			else{
				exit('Class does not exist.');
			}
		}
		else{
			exit('Model does not exist.');
		}
	}
	protected function redirect($url,$msg='',$time=0){
		$redirctURL=$this->getUrl($url);
		if(empty($msg)){
		header("Location:".$redirctURL);exit;
		}
		else{
			echo "<script type='text/javascript'>alert('$msg');window.location.href='".$redirctURL."';</script>";
		}
	}
	protected function getUrl($url){
		$path=trim($url,'/');
		$paths=explode('/',$path);
		$c=array_shift($paths);
		$a=array_shift($paths);
		$base_configs=$this->loadConfigs('base');
		if($base_configs['URL_MODE']==1){
			return "/index.php?c=$c&a=$a";
		}
		else if ($base_configs['URL_MODE']==2){
			return "/index.php/$c/$a";
		}
		else{
			//base configs error
		}
	}
	
}
//------------------------------------------------
//End of File : /Core/ZeegingBase.class.php
//------------------------------------------------